/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.maxato.controller;

import java.io.File;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import nl.maxato.domain.*;
import nl.maxato.gui.Main;
import nl.maxato.services.*;
import nl.maxato.util.ConfigurationFile;
import nl.maxato.util.ParameterNames;
import nl.maxato.xmlservices.ConnectToURL;
import nl.maxato.xmlservices.ConvertXMLData;
import nl.maxato.xmlservices.GetXMLData;
import nl.maxato.xmlservices.XMLService;

/**
 *
 * @author Fam Gloudemans
 */
public class Controller {

    
    CalculateDisciplines calculateDisciplines;
    CalculateGeneralData calculateGeneralData;
    CalculateDifferencesAthletes calculateDifferencesAthletes;
    CalculateWages calculateWages;
    ConfigurationFile configurationFile;
    Main main;
    XMLService xmlService;

    public Controller() {
        calculateDisciplines = new CalculateDisciplines();
        calculateGeneralData = new CalculateGeneralData();
        calculateDifferencesAthletes = new CalculateDifferencesAthletes();
        calculateWages = new CalculateWages();
        configurationFile = new ConfigurationFile();
        main = new Main();
        xmlService = XMLService.getInstance();
    }

  

    public List<Athlete> calculateDiscipline(List<Athlete> athletes, int disciplineString, int athletesSpecialty, int sex, boolean form, boolean care, boolean experience) {
        int discipline = calculateGeneralData.getDisciplineToInteger(disciplineString);
        int specialtyID = calculateGeneralData.getSpecialtyToInteger(athletesSpecialty);
        if (specialtyID != 7) {
            athletes = getAthleteBySpecialty(athletes, specialtyID);
        }
        if (sex != 0) {
            sex = sex - 1;
            athletes = getAthleteBySex(athletes, sex);
        }
        if (disciplinesBySpecialty(discipline, specialtyID)) {
            return calculateDisciplines.calculateDiscipline(athletes, discipline, form, care, experience);
        } else {
            return calculateDisciplines.calculateNewDiscipline(athletes, discipline, form, care, experience);
        }
    }

    public double getAveragedStatistics(List<Athlete> loa, String typeOfStatics) {
        return calculateGeneralData.getAveragedStatics(loa, typeOfStatics);
    }

    public Integer getTotalStatistics(List<Athlete> loa, String typeOfStatics) {
        return calculateGeneralData.getTotalStatics(loa, typeOfStatics);
    }

    /**
     * Filter the athletes list by specialty.
     * - 0 = No specialty
     * - 1 = Sprinters
     * - 2 = Middle Distance Runners
     * - 3 = Long Distance Runners
     * - 4 = Race Walkers
     * - 5 = Jumpers
     * - 6 = Throwers
     * @param listAthlete
     * @param SpecialtyID
     * @return 
     */
    public ArrayList<Athlete> getAthleteBySpecialty(List<Athlete> listAthlete, int SpecialtyID) {
        ArrayList<Athlete> listSortedBySpecialtyID = new ArrayList<Athlete>();
        for (int z = 0; z < listAthlete.size(); z++) {
            if (listAthlete.get(z).getSpecialtyId() == SpecialtyID) {
                listSortedBySpecialtyID.add(listAthlete.get(z));
            }
        }
        return listSortedBySpecialtyID;
    }

    public ArrayList<Athlete> getAthleteBySpecialtyAndSex(List<Athlete> listAthlete, int SpecialtyID, int Sex) {
        ArrayList<Athlete> listSortedBySpecialtyID = new ArrayList<Athlete>();
        for (int z = 0; z < listAthlete.size(); z++) {
            if (listAthlete.get(z).getSex() == Sex) {
                if (listAthlete.get(z).getSpecialtyId() == SpecialtyID) {
                    listSortedBySpecialtyID.add(listAthlete.get(z));
                }
            }
        }
        return listSortedBySpecialtyID;
    }

    /**
     * Calculate the specialty for an athlete without a specialty.
     * @param strenght
     * @param stamina
     * @param speed
     * @param agility
     * @param jump
     * @param throwing
     * @param rb
     * @return 
     */
    public List<Discipline> calculateSpecialty(String strenght, String stamina, String speed, String agility, String jump, String throwing, ResourceBundle rb) {
        return calculateDisciplines.calculateSpecialty(Integer.valueOf(strenght), Integer.valueOf(stamina), Integer.valueOf(speed), Integer.valueOf(agility), Integer.valueOf(jump), Integer.valueOf(throwing), rb);
    }

    public List<Athlete> getAthletesByDate(String date) {
        return xmlService.zetAtleetDataOmDoorDatum(date);

    }

    public List<Differences> compareXMLFiles(List<Athlete> list1, List<Athlete> list2, int sortBy, List<String> s) {
        return calculateDifferencesAthletes.compareXMLFiles(list1, list2, sortBy, s);
    }

    private boolean disciplinesBySpecialty(int discipline, int specialtyID) {
        if (specialtyID == ParameterNames.specialltyIdNoSpeciality) {
            return false;
        } else if (specialtyID == ParameterNames.specialltyIdSprinters) {
            if (discipline == ParameterNames.eventId100Metres || discipline == ParameterNames.eventId200Metres || discipline == ParameterNames.eventId400Metres || discipline == ParameterNames.eventId110Hurdles || discipline == ParameterNames.eventId400Hurdles) {
                return true;
            }
        } else if (specialtyID == ParameterNames.specialltyIdMiddleDistanceRunners) {
            if (discipline == ParameterNames.eventId800Metres || discipline == ParameterNames.eventId1500Metres || discipline == ParameterNames.eventId3000Steeplechase) {
                return true;
            }
        } else if (specialtyID == ParameterNames.specialltyIdLongDistanceRunners) {
            if (discipline == ParameterNames.eventId5000Metres || discipline == ParameterNames.eventId10000Metres || discipline == ParameterNames.eventIdMarathon) {
                return true;
            }
        } else if (specialtyID == ParameterNames.specialltyIdRaceWalkers) {
            if (discipline == ParameterNames.eventId10kmRaceWalk || discipline == ParameterNames.eventId20kmRaceWalk || discipline == ParameterNames.eventId50kmRaceWalk) {
                return true;
            }
        } else if (specialtyID == ParameterNames.specialltyIdJumpers) {
            if (discipline == ParameterNames.eventIdHighJump || discipline == ParameterNames.eventIdPoleVault || discipline == ParameterNames.eventIdLongJump || discipline == ParameterNames.eventIdTripleJump) {
                return true;
            }
        } else if (specialtyID == ParameterNames.specialltyIdThrowers) {
            if (discipline == ParameterNames.eventIdShotPut || discipline == ParameterNames.eventIdDiscusThrow || discipline == ParameterNames.eventIdHammerThrow || discipline == ParameterNames.eventIdJavelinThrow) {
                return true;
            }
        }
        return false;
    }

    private List<Athlete> getAthleteBySex(List<Athlete> athleteList, int sex) {
        List<Athlete> listSortedBySex = new ArrayList<Athlete>();
        for (Athlete athlete : athleteList) {
            if (athlete.getSex() == sex) {
                listSortedBySex.add(athlete);
            }
        }
        return listSortedBySex;
    }

    public ArrayList<String> getNextThreeCompetition(Calendar cv, ResourceBundle rb) {
        return calculateGeneralData.getNextThreeCompetition(cv, rb);
    }

//    public String setBestOnderdeel(int disc, List<Athlete> adf, int atleetGroep, int geslacht) {
//        //int discipline = bag.getDisciplineToInteger(disc);
//        //int specialtyID = bag.getSpecialtyToInteger(atleetGroep);
//        //if (specialtyID != 7) {
//        adf = getAthleteBySpecialty(adf, atleetGroep);
//        //  }
//        if (geslacht != 0) {
//            geslacht = geslacht - 1;
//            adf = getAthleteBySex(adf, geslacht);
//        }
//        return bd.getBestAthlete(disc, adf);
//    }
    public int getAgeDifference() {
        return calculateDifferencesAthletes.getAge();
    }

    public int getAgilityDifference() {
        return calculateDifferencesAthletes.getAgility();
    }

    public int getExperienceDifference() {
        return calculateDifferencesAthletes.getExperience();
    }

    public int getFansDifference() {
        return calculateDifferencesAthletes.getFans();
    }

    public int getHeightDifference() {
        return calculateDifferencesAthletes.getHeight();
    }

    public int getJumpDifference() {
        return calculateDifferencesAthletes.getJump();
    }

    public int getMaxidDifference() {
        return calculateDifferencesAthletes.getMaxid();
    }

    public int getSpecialty1Difference() {
        return calculateDifferencesAthletes.getSpecialty1();
    }

    public int getSpecialty2Difference() {
        return calculateDifferencesAthletes.getSpecialty2();
    }

    public int getSpeedDifference() {
        return calculateDifferencesAthletes.getSpeed();
    }

    public int getStaminaDifference() {
        return calculateDifferencesAthletes.getStamina();
    }

    public int getStrenghtDifference() {
        return calculateDifferencesAthletes.getStrenght();
    }

    public int getThrowingDifference() {
        return calculateDifferencesAthletes.getThrowing();
    }

    public int getWageDifference() {
        return calculateDifferencesAthletes.getWage();
    }

    public int getWeightDifference() {
        return calculateDifferencesAthletes.getWeight();
    }

    

    public List<Wage> calculateWageForNextSeason(Double maxid, Double fans, Double constant, List<Athlete> athletes, ResourceBundle rb) {
        return calculateWages.calculateWageForNextSeason(maxid, fans, constant, athletes, rb);
    }

    public ResourceBundle getLanguague(String languagues) {
        String language;
        String country;

        if (languagues.equals("nl")) {
            language = "nl";
            country = "NL";
        } else if (languagues.equals("en")) {
            language = "en";
            country = "EN";
        } else if (languagues.equals("et")) {
            language = "et";
            country = "EE";
        } else if (languagues.equals("ru")) {
            language = "ru";
            country = "RU";
        } else if (languagues.equals("de")) {
            language = "de";
            country = "DE";
        } else if (languagues.equals("es")) {
            language = "es";
            country = "ES";
        } else if (languagues.equals("fr")) {
            language = "fr";
            country = "FR";
        } else if (languagues.equals("pl")) {
            language = "pl";
            country = "PL";
        } else if (languagues.equals("pt")) {
            language = "pt";
            country = "PT";
        } else if (languagues.equals("cs")) {
            language = "cs";
            country = "CZ";
        } else if (languagues.equals("el")) {
            language = "el";
            country = "GR";
        } else if (languagues.equals("it")) {
            language = "it";
            country = "IT";
        } else if (languagues.equals("hu")) {
            language = "hu";
            country = "HU";
        } else {
            language = "en";
            country = "EN";
        }

        Locale locale = new Locale(language, country);
        ConfigurationFile.setCountry(country);
        ConfigurationFile.setLanguage(language);
        return ResourceBundle.getBundle("nl/maxato/translation/MessagesBundle", locale);
    }

    public String getAthletesNameByID(int id) {
        if(id == 0){
            return "Not Enough Athletes";
        }
        List<Athlete> athletes = xmlService.xmlToAthletes();
        String name = "";
        for (Athlete athlete : athletes) {
            if (athlete.getIdOfAthlete() == id) {
                name = athlete.getName() + " " + athlete.getSurname();
            }
        }
        return name;
    }
    
        public int getAthletesIdByName(String name) {
        if(name.equals("")){
            return 0;
        }
        List<Athlete> athletes = xmlService.xmlToAthletes();
        for (Athlete athlete : athletes) {
            String f =  athlete.getName() + " " + athlete.getSurname();
            if (f.equals(name)) {
                return athlete.getIdOfAthlete();
            }
        }
        return 0;
    }

    public ArrayList<Athlete> getAthletes() {
        return (ArrayList<Athlete>) xmlService.xmlToAthletes();
    }

    public String getNameFromCombobox(String name) {
        int i = name.indexOf("~");
        return name.substring(0, i - 1);
    }
}
